//显示和隐藏
$('.top-r_login').on({
    mouseout:function(){  
      $('.top-r_select').show() 
    },
   mouseleave:function(){
    $('.top-r_select').finish().hide() 
   } 
})
// 点击搜索改变边框
var searchBtn = document.querySelector('.search_btn')
searchBtn.onfocus = function (){
  searchBtn.style.borderColor = '#ff1268'
}
searchBtn.onblur = function(){
    searchBtn.style.borderColor = '#f8f8f8'
}

// 登录操作
if(getCookie('uname') === '' ){
    $('.top-r_login').click(function(){
        window.location.href = '../pages/login.html'
    })
}

var myname = getCookie('uname')
if( $('.top-r_login_lg').text() !== myname ){
$('.top-r_login_lg').text(`${myname}`)
$('.login_out').css('display','block')
 if( myname === ''){
    $('.top-r_login_lg').text('登录')
    $('.login_out').css('display','none') 
 }
}
$('.login_out').click(function(){
    $('.top-r_login_lg').text('登录')
    $('.login_out').css('display','none') 
    window.location.href = '../pages/index.html'
    setCookie('uname',1,-1)
})


// 判断是否登录,进去购物车
$('.top-r_sel_in').on('click','.cartbtn',function(){
    if( document.cookie === '' ){
        window.location.href = '../pages/login.html'
    }else{
        window.location.href = '../pages/carts.html'
    }
})



// 点击回到顶部
$(window).scroll(function(){
    // console.log()
    if($(window).scrollTop() >=240){
         $('.rtop').show()
    }else{
        $('.rtop').hide()
    }
})
$('.rtop').click(function(){
    // $(window).scrollTop()
  $('html').animate({
      scrollTop:0
  },300)
})
// 详情页信息
// 拿到存储的localStorage信息
let info  =JSON.parse(localStorage.getItem('goodsInfo'))
console.log(info)
// 判断信息是否存在
if( !info){
    $('.no-goods').show()
    $('.hd').hide()
}else{
    $('.no-goods').hide()
    $('.hd').show()
}
bindHtml()
function bindHtml(){
    var str = ''
    var str1 = ''
  $('.cover>img').attr('src',info.verticalPic)  
    str =`
    <span class="tip">总票代</span>
    <span>【${info.venuecity}】&nbsp;${info.name}</span>
    `
    $('.order-tit').html(str)
     str1 = `
     <div class="addre_time">时间 ：${info.showtime}
     </div>
     <div class="addre_place">
         <div class="place_addre">场馆 ： ${info.venuecity}市| ${info.venue}</div>
     </div>
     <div class="order-pice">票价 ：<span>${info.price}</span>元</div>
     `
     $('.order-addre').html(str1)
}
$('.buybtn').click(function(){
    if(document.cookie === ''){
        window.location.href = '../pages/login.html'
    }
  //加入到购物车数组里面
  let cartList = JSON.parse(localStorage.getItem('cartList')) || [] 
//   向数组里面把本条数据添加进去
  let exits = cartList.some(item =>{
      return item.id === info.id
  })
if(exits){
    // 表示有这个信息了, 我们要让 number ++
    // 找到这个信息给他 number ++
  let data = null
  for(let i =0 ;i<cartList.length;i++){
      if(cartList[i].id === info.id){
          data = cartList[i]
          break
      }
  }
   // data找到的这个信息
  data.number++
  //数量添加的时候, 小计价格要改变
  data.subtotal = data.number * data.price
}else{
    info.number = 1
    info.subtotal =info.price 
    info.isSelect = false
    cartList.push(info)
}
localStorage.setItem('cartList',JSON.stringify(cartList))
})

